package map.android.baidu.carpool.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.components.api.extra.APIProxy;

import map.android.baidu.carpool.R;

import java.util.ArrayList;

/**
 * Created by zhaoyangyang on 2015/3/26.
 * 使用说明
 */
public class InstructionsForUserDialog extends Dialog {
    private View mView = null;
    private TextView mTvContent = null;
    private Button mTvOk = null;
    private CallBack mCallBack = null;
    private Activity activity = null;
    private static final double SCALE = 1.0;
    private EditText mUserNameEtv = null;
    private UserinputInfo mUserinputInfo = new UserinputInfo();
    private int mSex = 2;

    public InstructionsForUserDialog(Activity activity, CallBack cal) {
        super(activity, R.style.TaxiTimeDialog);
        this.mCallBack = cal;
        this.activity = activity;
        initView();
    }

    public InstructionsForUserDialog(Activity activity, CallBack cal, int sex) {
        super(activity, R.style.TaxiTimeDialog);
        mSex = sex;
        this.mCallBack = cal;
        this.activity = activity;
        initView();
    }


    private void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mView = APIProxy.res().inflate(R.layout.dialog_carpool_instruction, null);
        setContentView(mView);
        WindowManager windowManager = this.activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.height = activity.getResources().getDimensionPixelSize(R.dimen.d_240);
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        mTvContent = (TextView) mView.findViewById(R.id.lin_dialog_instruction_content);
        mTvOk = (Button) mView.findViewById(R.id.lin_dialog_instruction_ok);
        mUserNameEtv = (EditText) findViewById(R.id.user_name_etv);

        final Button femaleTV = (Button) mView.findViewById(R.id.female_tv);
        final Button maleTV = (Button) mView.findViewById(R.id.male_tv);
        EditText userNameETV = (EditText) mView.findViewById(R.id.user_name_etv);
        initContent();
        if (mSex == 2) {
            femaleTV.setBackgroundResource(R.drawable.press_shurukuang);
            femaleTV.setTag("select");
            femaleTV.setTextColor(Color.WHITE);
            maleTV.setBackgroundResource(R.drawable.default_shurukuang);
            maleTV.setTag(null);
            maleTV.setTextColor(activity.getResources().getColor(R.color.car_text_gray_dack));
        } else{
            maleTV.setBackgroundResource(R.drawable.press_shurukuang);
            maleTV.setTag("select");
            maleTV.setTextColor(Color.WHITE);
            femaleTV.setBackgroundResource(R.drawable.default_shurukuang);
            femaleTV.setTag(null);
            femaleTV.setTextColor(activity.getResources().getColor(R.color.car_text_gray_dack));

        }

        femaleTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (femaleTV.getTag() != null) {
                } else {
                    femaleTV.setBackgroundResource(R.drawable.press_shurukuang);
                    femaleTV.setTag("select");
                    femaleTV.setTextColor(Color.WHITE);
                    maleTV.setBackgroundResource(R.drawable.default_shurukuang);
                    maleTV.setTag(null);
                    maleTV.setTextColor(activity.getResources().getColor(R.color.car_text_gray_dack));
                    mUserinputInfo.sex = 2;
                }

            }
        });

        maleTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (maleTV.getTag() != null) {
                } else {
                    maleTV.setBackgroundResource(R.drawable.press_shurukuang);
                    maleTV.setTag("select");
                    maleTV.setTextColor(Color.WHITE);
                    femaleTV.setBackgroundResource(R.drawable.default_shurukuang);
                    femaleTV.setTag(null);
                    femaleTV.setTextColor(activity.getResources().getColor(R.color.car_text_gray_dack));
                    mUserinputInfo.sex = 1;

                }

            }
        });

        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /*SharedPreferences sp = APIProxy.res().getBaseActivity().getSharedPreferences("first",
                        APIProxy.res().getBaseActivity().MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("isFirst", false);
                editor.commit();*/
                String userNameStr = mUserNameEtv.getText().toString();
                if (TextUtils.isEmpty(userNameStr)) {
                    ToastForCarpool.show(activity, "请填写您的姓氏");
                    return;
                }

                if (userNameStr.length() > 2) {
                    ToastForCarpool.show(activity, "姓氏超过最大长度");
                    return;
                }
                mUserinputInfo.name = userNameStr;
                dismiss();
                mCallBack.inputMessageComplete(mUserinputInfo);
            }
        });
    }

    private void initContent() {
            mTvContent.setText("乘客仅限一位,必须本人乘坐,完善信息\n提高接单机会");
    }

    public interface CallBack {
        public void inputMessageComplete(UserinputInfo userinputInfo);
    }

    public class UserinputInfo {

        public int sex = 2;
        public String name = "";
    }

}
